﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SmsSendData.cs" company="VDC-IT" author="Longlc">
//   SmsSendData class
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace VDC.VONE.DAL
{
    #region

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;

    using Microsoft.ApplicationBlocks.Data;

    using VDC.COMMON;
    using VDC.VONE.ENTITIES;

    #endregion

    /// <summary>
    /// The sms send data.
    /// </summary>
    public class SmsSendData
    {
        #region Public Methods and Operators

        /// <summary>
        ///     Gets all.
        /// </summary>
        /// <returns>return list SmsSendEN</returns>
        public IList<SmsSendEN> GetAll()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<SmsSendEN> lstSms = new List<SmsSendEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(
                    SqlHelper.ConnectionString, Constants.StorepProcedure.VOneSMSGetAll);
                while (reader.Read())
                {
                    SmsSendEN smsSend = new SmsSendEN();

                    if (!reader.IsDBNull(0))
                    {
                        smsSend.IdSms = reader.GetInt64(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        smsSend.Mtseq = reader.GetInt64(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        smsSend.Module = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        smsSend.Phone = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        smsSend.Message = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        smsSend.DateSent = reader.GetDateTime(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        smsSend.Status = reader.GetInt32(6);
                    }

                    lstSms.Add(smsSend);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstSms;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets all SMS.
        /// </summary>
        /// <param name="pageIndex">The start index.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="sortBy">The sort by.</param>
        /// <param name="total">The total sms.</param>
        /// <returns>return list SmsSendEN</returns>
        public IList<SmsSendEN> GetAllSmsPaging(int pageIndex, int pageSize, string sortBy, ref int total)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<SmsSendEN> lstSms = new List<SmsSendEN>();
                SqlParameter startIndexParam = new SqlParameter("@pageIndex", SqlDbType.Int);
                startIndexParam.Value = pageIndex;
                SqlParameter pageSizeParam = new SqlParameter("@pageSize", SqlDbType.Int);
                pageSizeParam.Value = pageSize;
                SqlParameter sortByParam = new SqlParameter("@sortBy", SqlDbType.NVarChar, 50);
                sortByParam.Value = sortBy;
                SqlParameter totalParam = new SqlParameter("@totalSms", SqlDbType.Int);
                totalParam.Direction = ParameterDirection.Output;

                SqlDataReader reader = SqlHelper.ExecuteReader(
                    SqlHelper.ConnectionString, CommandType.StoredProcedure, Constants.StorepProcedure.VOneSMSGetAllPaging, startIndexParam, pageSizeParam,sortByParam, totalParam);
                
                while (reader.Read())
                {
                    SmsSendEN smsSend = new SmsSendEN();

                    if (!reader.IsDBNull(0))
                    {
                        smsSend.IdSms = reader.GetInt64(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        smsSend.Mtseq = reader.GetInt64(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        smsSend.Module = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        smsSend.Phone = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        smsSend.Message = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        smsSend.DateSent = reader.GetDateTime(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        smsSend.Status = reader.GetInt32(6);
                    }

                    lstSms.Add(smsSend);
                }

                reader.Close();

                total = Convert.ToInt32(totalParam.Value);

                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstSms;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets all count.
        /// </summary>
        /// <returns>return int</returns>
        public int GetAllCount()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                
                object value = SqlHelper.ExecuteScalar(
                    SqlHelper.ConnectionString, "[VONE_SMS_GET_Total_Count]");
                
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return Convert.ToInt32(value);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        #endregion
    }
}